package com.meepo.usercenter.profile

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.lxj.xpopup.XPopup
import com.meepo.basic.router.RouterManager
import com.meepo.basic.auth.Store
import com.meepo.basic.components.LocationManager
import com.meepo.basic.nightmode.NightModeHelper
import com.meepo.basic.schema.user.User
import com.meepo.basic.components.view.MeepoActivity
import com.meepo.framework.design.helper.ImageLoader
import com.meepo.framework.design.helper.StatusBarHelper
import com.meepo.usercenter.API
import com.meepo.usercenter.R
import com.xiaojinzi.component.anno.RouterAnno
import kotlinx.android.synthetic.main.userinfo_edit_activity.*

@RouterAnno(hostAndPath = RouterManager.UserModule.UserInfoEdit_Path)
class UserInfoEditActivity : MeepoActivity() {



    private lateinit var mUserInfo: User

    @SuppressLint("SetTextI18n")
    private fun applyUserInfo(user: User) {

        ImageLoader.loadImage(this, user.avatar, avatar_ci)

        nickname_tv.text = user.nickname
        nickname_ll.setOnClickListener { updateNickname() }

        sign_tv.text = user.sign
        sign_ll.setOnClickListener {
            SignActivity.launch(this, user.sign ?: "")
        }

        if (user.gender != null) {
            gender_tv.text = user.gender
        } else {
            gender_tv.text = "点击设置性别"
        }
        gender_ll.setOnClickListener { updateGender() }

//        if (user.birthday != null) {
//            birthday_tv.text = user.birthday
//        } else {
//            birthday_tv.text = "点击设置生日"
//        }

        if (user.location.isNullOrEmpty()) {
            location_tv.text = "点击设置城市"
        } else {
            location_tv.text = user.location
        }

        location_ll.setOnClickListener { updateLocation() }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        val value = data?.getStringExtra("sign") ?: ""

        when (resultCode) {
            1 -> updateUserInfo("sign", value) { mUserInfo.sign = value; applyUserInfo(mUserInfo) }
        }
    }

    private fun updateLocation() {

        val provinces = LocationManager.getProvinces()
        val cities = LocationManager.getCities()

        val picker = OptionsPickerBuilder(this, OnOptionsSelectListener { options1, options2, _, _ ->
            val location: String = provinces[options1].pickerViewText + "-" + cities[options1][options2]
            updateUserInfo("location", location) { mUserInfo.location = location;applyUserInfo(mUserInfo) }

        })
            .setTitleText("城市选择").build<Any>()

        picker.setPicker(provinces, cities)
        picker.show()
    }

    private fun updateGender() {
        XPopup.Builder(this)
            .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
            .isDarkTheme(NightModeHelper.isDarkMode(this))
            .asBottomList(
                "选择性别", arrayOf("男", "女"), null, if (mUserInfo.gender == "男") 0 else 1
            ) { _, text -> updateUserInfo("gender", text) { mUserInfo.gender = text; applyUserInfo(mUserInfo) } }
            .show()
    }

    private fun updateNickname() {
        XPopup.Builder(this)
            .hasStatusBarShadow(false) //.dismissOnBackPressed(false)
            .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
            .autoOpenSoftInput(true)
            .isDarkTheme(NightModeHelper.isDarkMode(this))
            //.moveUpToKeyboard(false)   //是否移动到软键盘上面，默认为true
            .asInputConfirm(
                "修改昵称", "", mUserInfo.nickname ?: "", "请设置昵称"
            ) { text -> updateUserInfo("nickname", text) { mUserInfo.nickname = text; applyUserInfo(mUserInfo) } }
            .show()
    }

    private fun updateUserInfo(key: String, value: String, onSuccess: () -> Unit) {
        presenter.call(
            API.instance().updateUser(key, value),
            { onSuccess() }
        )
    }

    override fun initData() {
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        StatusBarHelper.immerse(this, header_rl)

        Store.Func.getUserSummary()?.id?.let {
            presenter.call(
                API.instance().getUserInfo(it),
                { rsp ->
                    if (rsp.data == null) finish() else {
                        applyUserInfo(rsp.data!!); mUserInfo = rsp.data!!; }
                },
                { finish() }
            )
        }




        back_iv.setOnClickListener { finish() }


    }

    override fun setUpContentLayout(): Int = R.layout.userinfo_edit_activity

}